/*
 * buffer.cpp
 *
 *  Created on: Jul 13, 2017
 *      Author: zhangzm
 */
#include "buffer.h"

IdentityPool::IdentityPool()
{
	this->id_pool = NULL;
	this->unit_size = 0;
	LOG_FATAL("run");
	exit(0);
}

IdentityPool::IdentityPool(int capacity, Ctor ctor, Dtor dtor, int unit_size)
{
	LOG_DEBUG("run capacity %d", capacity);
	this->id_pool = identity_pool_new(capacity, ctor, dtor);
	this->unit_size = unit_size;
}

IdentityPool::~IdentityPool()
{
	LOG_DEBUG("run");
	identity_pool_delete(this->id_pool);
}

void* IdentityPool::pmalloc(uint32_t size)
{
	LOG_DEBUG("run malloc %d", size);
	if(size > this->unit_size){
		LOG_ERROR("error MEMPOOL_BUFFER_SIZE is %d and input size is %d", this->unit_size, size);
		return NULL;
	}
	return identity_pool_get(this->id_pool);
}

void IdentityPool::pfree(void* p)
{
	LOG_DEBUG("run");
	identity_pool_put(this->id_pool, p);
}


RingBuffer::RingBuffer(uint64_t ring_size, uint64_t alignment, void * ptr)
{
	LOG_DEBUG("run ring_size %ld alignment %ld", ring_size, alignment);
	this->ringbuf = ursax_ringbuf_create(ring_size, alignment, ptr);
}

RingBuffer::RingBuffer()
{
	this->ringbuf = NULL;
	LOG_DEBUG("run");
	exit(0);
}

RingBuffer::~RingBuffer()
{
	ursax_ringbuf_destroy(this->ringbuf);
}

void* RingBuffer::pmalloc(uint32_t size)
{
	LOG_DEBUG("run size %d", size);
	return ursax_ringbuf_malloc(this->ringbuf, size);
}

void RingBuffer::pfree(void* p)
{
	LOG_DEBUG("run");
	ursax_ringbuf_free(this->ringbuf, p);
}

